package com.newtype.meeting.client.widget.backend;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.service.AccountServiceAsync;
import com.newtype.meeting.shared.AccountSummaryDTO;

/**
 * 人员维护面板控件
 * @author 汪恭泽
 * May 31, 2011 10:57:12 AM
 */
public class AccountManagementPanel extends ContentPanel {
	private AccountServiceAsync accountService = Registry
			.get(GMeeting.ACCOUNT_SERVICE);

	public AccountManagementPanel() {
		setHeading("人员维护");
		setSize(800, 700);
		setIconStyle("icon-system");
	}

	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		setLayout(new FitLayout());

		//proxy
		RpcProxy<PagingLoadResult<AccountSummaryDTO>> proxy = new RpcProxy<PagingLoadResult<AccountSummaryDTO>>() {
			@Override
			protected void load(Object loadConfig,
					AsyncCallback<PagingLoadResult<AccountSummaryDTO>> callback) {
				accountService.getAccounts((PagingLoadConfig) loadConfig, "0",
						callback);
			}
		};

		//loader
		final BasePagingLoader<PagingLoadResult<ModelData>> loader = new BasePagingLoader<PagingLoadResult<ModelData>>(
				proxy, new BeanModelReader());
		loader.setRemoteSort(true);

		final ListStore<BeanModel> store = new ListStore<BeanModel>(loader);

		final PagingToolBar toolBar = new PagingToolBar(50);
		toolBar.bind(loader);

		RowNumberer r = new RowNumberer();

		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(r);//添加行号
		//添加选择框
		CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
		columns.add(sm.getColumn());

		ColumnConfig id = new ColumnConfig("id", "序号", 50);
		id.setHidden(true);
		columns.add(id);

		columns.add(new ColumnConfig("accountID", "帐号", 200));
		columns.add(new ColumnConfig("fullName", "姓名", 200));

		ColumnConfig column = new ColumnConfig("gender", "性别", 150);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setRenderer(new GridCellRenderer<BeanModel>() {
			@Override
			public Object render(BeanModel model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<BeanModel> store, Grid<BeanModel> grid) {
				return model.get(property).equals("1") ? "男" : "女";
			}
		});
		columns.add(column);

		column = new ColumnConfig("onlineDATE", "入职日期", 200);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(column);

		column = new ColumnConfig("registerDate", "申请时间", 200);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setDateTimeFormat(DateTimeFormat
				.getFormat("yyyy-MM-dd HH:mm:ss"));
		columns.add(column);

		ColumnModel cm = new ColumnModel(columns);

		final Grid<BeanModel> grid = new Grid<BeanModel>(store, cm);
		grid.setStateful(true);
		grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {
			@Override
			public void handleEvent(GridEvent<BeanModel> be) {
				loader.load(0, 50);
			}
		});

		grid.setAutoExpandColumn("accountID");
		grid.addPlugin(r);
		grid.addPlugin(sm);
		grid.getView().setForceFit(true);
		grid.setBorders(true);
		grid.setLoadMask(true);

		grid.setSelectionModel(sm);
		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		ToolBar headToolBar = new ToolBar();
		ToggleButton toggle = new ToggleButton("开通");
		toggle.setIconStyle("icon-edit");
		toggle.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (grid.getSelectionModel().getSelectedItem() == null
						|| grid.getSelectionModel().getSelectedItems().size() <= 0) {
					MessageBox.alert("提示", "请选择一条记录", null);
				} else {
					AccountSummaryDTO account = grid.getSelectionModel()
							.getSelectedItem().getBean();
					Dispatcher.forwardEvent(AppEvents.ToOpenedNewAccount,
							account);
				}
			}
		});
		headToolBar.add(toggle);

		ContentPanel panel = new ContentPanel();
		panel.setFrame(true);
		panel.setCollapsible(true);
		panel.setAnimCollapse(false);
		panel.setButtonAlign(HorizontalAlignment.CENTER);
		panel.setHeading("待开通帐号列表");
		panel.setLayout(new FitLayout());
		panel.add(grid);
		panel.setSize(600, 350);
		panel.setTopComponent(headToolBar);
		panel.setBottomComponent(toolBar);

		add(panel);
	}
}
